/*
 * Created on 2005-3-21
 *
 */
package com.hd123.rumba.sql;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import com.hd123.rumba.exception.BusinessException;

/**
 * @author caili
 */
public class ResultPage {
  public static ResultPage createJdbcResultPage(Connection connection, SQLSubquery sq,
      int pageSize, int page) throws BusinessException, HibernateException {
    SelectExecutor worker = new JdbcSelect(connection, sq, pageSize);
    ResultPage rp = new ResultPage(worker);
    rp.setPagePageSize(pageSize, page);
    return rp;
  }

  /**
   * @param connection
   * @param sq
   * @param start
   *          起始记录号
   * @param limit
   *          记录数. 0 表示全部记录.
   * @return
   * @throws BusinessException
   * @throws HibernateException
   */
  public static ResultPage createJdbcResultAsOnePage(Connection connection, SQLSubquery sq,
      int start, int limit) throws BusinessException, HibernateException {
    SelectExecutor worker = new JdbcSelect(connection, sq, 0);
    ResultPage rp = new ResultPage(worker);
    rp.setStartLimit(start, limit);
    return rp;
  }

  public static ResultPage createHibernateResultPage(Session session, SQLSubquery sq, int pageSize,
      int page) throws HibernateException {
    SelectExecutor worker = new HibernateSelect(session, sq, pageSize);
    ResultPage rp = new ResultPage(worker);
    rp.setPagePageSize(pageSize, page);
    return rp;
  }

  private ResultPage(SelectExecutor aWorker) {
    worker = aWorker;
  }

  private void setPagePageSize(int aPageSize, int aPage) throws HibernateException {
    pageSize = aPageSize;
    page = aPage;
    elementCount = worker.getCount();
    if (pageSize <= 0)
      pageSize = elementCount;
    if (pageSize <= 0)
      pageSize = 1;
    pageCount = elementCount / pageSize;
    if (pageCount * pageSize < elementCount)
      pageCount++;
    if (page < 0)
      page = 0;
    else if (page > pageCount - 1)
      page = pageCount - 1;
    pageMode = true;
  }

  private void setStartLimit(int start, int limit) throws HibernateException {
    page = 0;
    elementCount = worker.getCount();
    pageCount = 1;
    pageSize = limit == 0 ? elementCount : limit;
    pageMode = false;
    this.start = start;
    this.limit = limit;
  }

  private SelectExecutor worker = null;
  private int page;
  private int pageSize;
  private int elementCount;
  private int pageCount;
  private String id;
  private boolean pageMode = true; // true标识使用page, pageSize; false表示使用start,
                                    // limit
  private int start;
  private int limit;

  public Iterator getContentIterator() throws BusinessException, HibernateException {
    return worker.getContentIterator(getPageStart() - 1, getPageSize());
  }

  public List getContentList() throws BusinessException, HibernateException {
    return worker.getContentList(getPageStart() - 1, getPageSize());
  }

  public int getPage() {
    return page;
  }

  public int getPageSize() {
    return pageSize;
  }

  public int getElementCount() {
    return elementCount;
  }

  public int getPageCount() {
    return pageCount;
  }

  public int getPageStart() {
    if (pageMode)
      return page * pageSize + 1;
    else
      return start;
  }

  public int getPageEnd() {
    if (pageMode) {
      int i = getPageStart() + pageSize - 1;
      return i > getElementCount() ? getElementCount() : i;
    } else
      return start + limit - 1;
  }

  public int getPrevPage() {
    return isFirstPage() ? page : page - 1;
  }

  public int getNextPage() {
    return isLastPage() ? page : page + 1;
  }

  public boolean isFirstPage() {
    return page == 0;
  }

  public boolean isLastPage() {
    return page == pageCount - 1;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }
}
